package org.example.struct.array2;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liyishan
 * @date 2024/7/1 09:42
 * @apiNote
 */

public class Test00006Convert {

    public static void main(String[] args) {
        String s ="PAYPALISHIRING";
        int numRows = 4;
        System.out.println(convert(s, numRows));
    }

    public static String convert(String s, int numRows) {
        if (numRows < 2) {
            return s;
        }
        List<StringBuilder> rows = new ArrayList<>();
        for (int i = 0; i < numRows; i++) {
            rows.add(new StringBuilder());
        }
        int i = 0, flag = -1;
        for (char c : s.toCharArray()) {
            rows.get(i).append(c);
            if(i == 0 || i == numRows -1){
                flag = -flag;
            }
            i +=flag;
        }
        StringBuilder res = new StringBuilder();
        for(StringBuilder row:rows){
            res.append(row);
        }
        return res.toString();
    }

}
